package threshold;

import java.util.Collections;
import java.util.List;

public class RatioNDCRThresholdFinder extends MaxNDCRThresholdFinder {

	private double ratio;
	
	@Override
	public List<String> initialize(List<String> args) {
		ratio = Double.parseDouble(args.get(0));
		return args.subList(1, args.size());
	}
	
	@Override
	public double find(List<Integer> label, List<Double> predicted) {
		List<Pair> pairList = getPairs(label, predicted);
		Collections.sort(pairList, comp);
		
		int positive = 0;
		for(int l : label) {
			if(l == 1) {
				positive ++;
			}
		}
		
		int mininum = (int)(ratio * positive);
		
		int poscount = 0;
		int count = 0;
		Pair lastPair = pairList.get(0);
		for(Pair pair : pairList) {
			if(pair.label == 1) {
				poscount ++;
				if(poscount >= mininum) {
					lastPair = pair;
					break;
				}
			}
			count ++;
		}
		System.out.println("minimum = " + mininum + " rank = " + count);
		
		return lastPair.logProb;
	}
}
